package com.hjf.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.commons.po.DeptPo;
import com.commons.po.MenuPo;
import com.commons.po.PageBean;
import com.dao.BaseDao;
import com.hjf.dao.MenuPoDao;

public class MenuPoDaoImpl extends BaseDao<MenuPo> implements MenuPoDao {

	@Override
	public List<MenuPo> findMenuPoAll(Map<String, Object> map) { //map:查询条件、条件的值
		List<Object> paramList=new ArrayList<>();
		StringBuffer sf=new StringBuffer();
		sf.append(" select * from menu where 1=1 ");
		//加入条件判断
		if(map!=null && map.size()>0){
			//指定菜单id
			if(map.get("menuId")!=null){
				sf.append(" and menu_id=? ");
				paramList.add(map.get("menuId"));
			}
			//查询不等于指定id的菜单
			if(map.get("exincludeMenuId")!=null){
				sf.append(" and menu_id<>? ");
				paramList.add(map.get("exincludeMenuId"));
			}
		}
		return this.executeQuery(sf.toString(), null, MenuPo.class, paramList.toArray());
	}

	@Override
	public MenuPo findMenuPoById(Integer id) {
		String sql="select * from menu where menu_id=?";
		List<MenuPo> list=this.executeQuery(sql, null, MenuPo.class, id);
		return (list!=null && list.size()>0)?list.get(0):null;
	}

	@Override
	public List<MenuPo> findByMenuPoParent(Integer deptParent) {
		String sql="select * from menu where menu_parent=?";
		List<MenuPo> list=this.executeQuery(sql, null, MenuPo.class, deptParent);
		return list;
	}

	@Override
	public int saveMenuPo(MenuPo entity) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int updateMenuPo(MenuPo entity) {
		String sql="update menu set menu_name=?,menu_icon=?,menu_url=?,created_id=?,created_date=?,updated_id=?,updated_date=?,is_active=?,menu_parent=? where menu_id=?";
		List<Object> paramsObjs=new ArrayList<Object>();
		paramsObjs.add(entity.getMenuName());
		paramsObjs.add(entity.getMenuIcon());
		paramsObjs.add(entity.getMenuUrl());
		paramsObjs.add(entity.getCreatedId());
		paramsObjs.add(entity.getCreatedDate());
		paramsObjs.add(entity.getUpdatedId());
		paramsObjs.add(entity.getUpdatedDate());
		paramsObjs.add(entity.getIsActive());
		paramsObjs.add(entity.getMenuParent());
		paramsObjs.add(entity.getMenuId());
		return this.executeUpdate(sql, paramsObjs.toArray());
	}

	@Override
	public PageBean<MenuPo> findPageByCondition(int pageIndex, int pageSize, Map<String, Object> map) {
		//1、先初始分页对象并设置初始化属性
				PageBean<MenuPo> pagebean=new PageBean<MenuPo>();
				pagebean.setPageIndex(pageIndex);
				pagebean.setPageSize(pageSize);
				List<Object> paramList=new ArrayList<Object>();
				
				//2、拼接sql
				StringBuffer baseSql=new StringBuffer();
				baseSql.append(" select * from menu where 1=1 ");
//				Integer parent=null;
				if(map!=null&&map.size()>0){
//					baseSql.append(" and menu_parent=? ");
//					parent=(Integer)map.get("menuId");
//					paramList.add(map.get("menuId"));
				}else{
//					baseSql.append(" and menu_parent is null ");
				}
				
				//3、获得总记录
				pagebean.setTotalCount(this.findMenuPoAll(null).size()); //pagebean.setTotalCount(this.getCount(baseSql.toString(), paramList.toArray()));
				
				//4、获得分页数据
				if(pagebean.getTotalCount()>0){
					StringBuffer pageSql=new StringBuffer();
					pageSql.append(" select * from ( ");
					pageSql.append("  select tmp1.*,rownum rn from ( ");
					pageSql.append(baseSql.toString());
					pageSql.append("  ) tmp1");
					pageSql.append(" ) tmp2");
					pageSql.append(" where tmp2.rn>? and tmp2.rn<=? ");
					int startRow=(pagebean.getPageIndex()-1)*pagebean.getPageSize();
					int endRow=pagebean.getPageIndex()*pagebean.getPageSize();
					paramList.add(startRow);//开始行号
					paramList.add(endRow);//结束行号
					
//					Map<String, String> columnsMap=new HashMap<String, String>();
//					columnsMap.put("menu_id", "id");//列名与属性名不相同时
					List<MenuPo> l=this.executeQuery(pageSql.toString(), null, MenuPo.class, paramList.toArray());
					pagebean.setList(this.executeQuery(pageSql.toString(), null, MenuPo.class, paramList.toArray()));
				}
				return pagebean;
	}

}
